﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using GrayParrot.Web.Controls.Datatables.Filters;
using GrayParrot.Extensions.Strings;

namespace GrayParrot.Web.Controls//.Datatables.Filters
{
    [Serializable]
    public class DataItem
    {
        /// <summary>
        /// Gets or sets a value indicating whether this instance is default.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is default; otherwise, <c>false</c>.
        /// </value>
        public bool IsDefault { get; set; }
        /// <summary>
        /// Gets or sets the name...
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public string Name { get; set; }
        /// <summary>
        /// Gets or sets the value...
        /// </summary>
        /// <value>
        /// The value.
        /// </value>
        public string Value { get; set; }

        public DataItem()
        {
        }

        public DataItem(string name, string value)
        {
            this.Name = name;
            this.Value = value;
        }
    }

    [Serializable]
    public class CustomChoiceFilter : ColumnFilter
    {
        private List<DataItem> m_data = new List<DataItem>();

        public string FilterDefaultLabel { get; set; }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty)]
        public List<DataItem> Data     
        {
            get 
            {
                if (m_data == null)
                {
                    m_data = new List<DataItem>();
                }

                return m_data; 
            }
            set { m_data = value; }
        }

        public CustomChoiceFilter()
        {
            this.Type = FilterTypes.CustomChoice;
        }

        public DataItem GetDefault()
        {
            DataItem retValue = null;

            retValue = this.Data.Where(x => x.IsDefault).FirstOrDefault();
            if (retValue == null)
            {
                retValue = new DataItem("", "");
            }

            return retValue;
        }

        public string BuildData()
        {
            StringBuilder m_dataSB = new StringBuilder();
            foreach (DataItem item in this.Data)
            {
                m_dataSB.AppendFormat("{{value: '{0}', label: '{1}'}},", item.Value, item.Name);
            }

            string m_data = m_dataSB.ToString();
            m_data.ReplaceLastOccurrence(",", "");

            return m_data;
        }
    }
}
